package com.ipbd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ipbd.domain.BreathRecord;
import com.ipbd.mapper.BreathRecordMapper;
import com.ipbd.service.BreathRecordService;
import com.ipbd.utils.R;
import com.ipbd.utils.UserHolder;
import com.ipbd.vo.BreathRecordVO;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
* @author 86172
* @description 针对表【breath_record(呼吸记录表)】的数据库操作Service实现
* @createDate 2024-03-06 20:53:48
*/
@Service
public class BreathRecordServiceImpl extends ServiceImpl<BreathRecordMapper, BreathRecord> implements BreathRecordService{

    /**
     * 添加呼吸记录表
     * @param breathRecord
     * @return
     */
    @Override
    public R saveBreathRecord(BreathRecord breathRecord) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        BreathRecord newBreathRecord = new BreathRecord();
        // 复制属性值
        BeanUtil.copyProperties(breathRecord, newBreathRecord);
        newBreathRecord.setUserId(id);
        // 保存到数据库中
        save(newBreathRecord);
        return R.ok("添加成功");
    }

    /**
     * 查询呼吸记录表
     * @return
     */
    @Override
    public R selectBreathRecord() {
        // 获取用户id
        //Integer id = UserHolder.getUser().getId();
        Integer id = 1;
        // 获取当天的日期
        LocalDate today = LocalDate.now();
        Date dateToday = Date.valueOf(today);

        List<BreathRecord> breathRecords = query()
                .eq("user_id", id)
                .eq("train_time", dateToday)
                .list();
        // 创建 SimpleDateFormat 实例进行日期格式化
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedToday = formatter.format(dateToday);

        List<BreathRecordVO> breathRecordVOS = BeanUtil.copyToList(breathRecords, BreathRecordVO.class);
        for (BreathRecordVO breathRecordVO : breathRecordVOS) {
            breathRecordVO.setTrainTime(formattedToday);
        }
        return R.ok().put("breathRecord", breathRecordVOS);
    }

    @Override
    public R selectAllBreathRecord() {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        List<BreathRecord> breathRecords = query()
                .eq("user_id", id)
                .list();
        // 创建 SimpleDateFormat 实例进行日期格式化
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        List<BreathRecordVO> breathRecordVOS = new ArrayList<>();
        for (BreathRecord breathRecord : breathRecords) {
            BreathRecordVO breathRecordVO = new BreathRecordVO();
            BeanUtil.copyProperties(breathRecord, breathRecordVO);
            breathRecordVO.setTrainTime(formatter.format(breathRecord.getTrainTime()));
            breathRecordVOS.add(breathRecordVO);
        }
        return R.ok().put("breathAllRecord", breathRecordVOS);
    }

    /**
     * 获取用户呼吸训练次数
     * @return
     */
    @Override
    public R getBreathCount() {
        Integer id = UserHolder.getUser().getId();
        Long count = query().eq("user_id", id).count();
        return R.ok().put("count", count);
    }
}




